﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace ECCSystem
{
    public partial class frm_Ext_Report_OFFAnnotation : Form
    {
        public frm_Ext_Report_OFFAnnotation()
        {
            InitializeComponent();
        }
        private DataSet objDS;
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.dateTimePicker_From.Value.CompareTo(this.dateTimePicker_To.Value) == 1)
            {

            }

            var objBRReports = new ECCSystem.BizRule.ClsReports();
            objDS = (DataSet)objBRReports.GetOFFAnnotationReport(this.dateTimePicker_From.Value
                                                                , this.dateTimePicker_To.Value);

            this.CreateExcel(objDS.Tables[0]);
        }


        //excel operation(open,fill,quit)
        private void CreateExcel(DataTable dt)
        {
            Excel.ApplicationClass objExlApp = new Excel.ApplicationClass();
            Excel.Workbook theWookbook = null;
            Excel.Worksheet theWookSheet = null;
            var objCommon = new ECCSystem.BizRule.ClsCommon();
            try
            {
                objExlApp.Visible = false;

                object objMissing = System.Reflection.Missing.Value;
                StringBuilder strSavePath = new StringBuilder();
                strSavePath.Append("C:\\ECCReport\\");
                //if strSavePath is not existing,create the path
                if (Directory.Exists(strSavePath.ToString()) == false)
                {
                    Directory.CreateDirectory(strSavePath.ToString());
                }

                StringBuilder strTemplatePath = new StringBuilder();
                //Excel template path
                strTemplatePath.Append(System.Windows.Forms.Application.StartupPath);
                strTemplatePath.Append("\\Ext\\ReportTemplate\\Ext_Report_OFFAnnotation.xls");
                //if this template is existing or not
                if (File.Exists(strTemplatePath.ToString()) == false)
                {
                    MessageBox.Show("レポートのテンプレートファイルが見つかりません。", "ECC Information");
                    //quit excel object
                    objCommon.QuitExcelApp(objExlApp, theWookbook, theWookSheet, null);
                    System.GC.Collect();
                    //this.btn_Export.Enabled = true;
                    return;
                }
                //open the excel template
                theWookbook = objExlApp.Workbooks.Open(strTemplatePath.ToString(), objMissing, objMissing, objMissing, objMissing, objMissing,
                                         objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing);
                //get the first sheet
                theWookSheet = (Excel.Worksheet)theWookbook.Worksheets[1];

                //fill data into excel
                string[] tmpTypes;
                int baseInx = 2;
                for (var k = 0; k < dt.Rows.Count; k++)
                {
                    tmpTypes = new string[7];
                    tmpTypes[0] = dt.Rows[k]["CaseNo"].ToString();
                    tmpTypes[1] = dt.Rows[k]["AccountNo"].ToString();
                    tmpTypes[2] = dt.Rows[k]["RecordUser"].ToString();
                    tmpTypes[3] = dt.Rows[k]["RecordDate"].ToString();
                    tmpTypes[4] = dt.Rows[k]["FollowUser"].ToString();
                    tmpTypes[5] = dt.Rows[k]["FollowDate"].ToString();
                    tmpTypes[6] = dt.Rows[k]["Name"].ToString();

                    theWookSheet.Range["A" + (k + baseInx) + ":G" + (k + baseInx)].Value2 = tmpTypes;
                }

                strSavePath.Append("OFF注文二次"
                                    + this.dateTimePicker_From.Value.Date.ToString("yyyy-MM-dd")
                                    + "-" + this.dateTimePicker_To.Value.Date.ToString("yyyy-MM-dd"));


                if (File.Exists(strSavePath.ToString() + ".xls") == true)
                {
                    File.Delete(strSavePath.ToString() + ".xls");
                }

                //if the report has existed or not
                if (File.Exists(strSavePath.ToString()) == true)
                {
                    MessageBox.Show("このファイルがもう存在しました！", "ECC Information");
                    //quit excel object
                    objCommon.QuitExcelApp(objExlApp, theWookbook, theWookSheet, null);
                    System.GC.Collect();
                    //btnExcel.Enabled = true;
                    return;
                }

                //save the report
                theWookbook.SaveAs(strSavePath.ToString(), objMissing, objMissing, objMissing, objMissing, objMissing,
                    Excel.XlSaveAsAccessMode.xlNoChange, objMissing, objMissing, objMissing, objMissing);
                //prompt save success
                MessageBox.Show("操作成功しました。", "ECC Information");

                //quit excel object
                objCommon.QuitExcelApp(objExlApp, theWookbook, theWookSheet, null);
                System.GC.Collect();
                this.Close();
            }
            catch (Exception ex)
            {
                //quit excel object
                objCommon.QuitExcelApp(objExlApp, theWookbook, theWookSheet, null);
                System.GC.Collect();

                //MessageBox.Show(ex.ToString(), "ECC Catch Error");
                //btnExcel.Enabled = true;
            }
        }
    }
}
